﻿// -----------------------------------------------------------------------
// <copyright file="ITimeTrackingContract.cs" company="Spardo, LLC">
// Copyright (c) 2011 SPardo, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
// </copyright>
// -----------------------------------------------------------------------

namespace DotNet37Signals.Client.Basecamp.Contracts
{
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using DotNet37Signals.Client.Basecamp.Models;
using System.Linq.Expressions;
using System;
    using DotNet37Signals.Client.Basecamp.Linq;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    [ServiceContract]
    public interface ITimeTrackingContract : IBasecampContract
    {
        [OperationContract]
        [WebGet(UriTemplate = "/projects/{projectId}/time_entries.xml")]
        TimeEntries GetList(string projectId);

        [OperationContract]
        [WebGet(UriTemplate = "/todo_items/{todoItemId}/time_entries.xml")]
        TimeEntries GetListByTodoItem(string todoItemId);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/projects/{projectId}/time_entries.xml")]
        NewEntity Create(string projectId, TimeEntryNew item);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/todo_items/{todoItemId}/time_entries.xml")]
        NewEntity CreateForTodoItem(string todoItemId, TimeEntryNew item);

        [OperationContract]
        [WebGet(UriTemplate = "/projects/{projectId}/time_entries/new.xml")]
        TimeEntryNew New(string projectId);

        [OperationContract]
        [WebGet(UriTemplate = "/time_entries/{timeEntryId}.xml")]
        TimeEntry Get(string timeEntryId);

        [OperationContract]
        [WebGet(UriTemplate = "/time_entries/{timeEntryId}/edit.xml")]
        TimeEntryNew Edit(string timeEntryId);

        [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "/time_entries/{timeEntryId}.xml")]
        void Update(string timeEntryId, TimeEntryNew item);

        [OperationContract]
        [WebInvoke(Method = "DELETE", UriTemplate = "/time_entries/{timeEntryId}.xml")]
        void Delete(string timeEntryId);

        [OperationContract]
        [WebGet(UriTemplate = "/time_entries/{criteria}")]
        TimeEntries Report(string criteria);
    }

    public static class TimeTrackingContractExtensions
    {
        public static TimeEntries GetList(this ITimeTrackingContract contract, Int64 projectId)
        {
            return contract.GetList(projectId.ToString());
        }

        public static TimeEntries GetListByTodoItem(this ITimeTrackingContract contract, Int64 todoItemId)
        {
            return contract.GetListByTodoItem(todoItemId.ToString());
        }

        public static NewEntity Create(this ITimeTrackingContract contract, Int64 projectId, TimeEntryNew item)
        {
            return contract.Create(projectId.ToString(), item);
        }

        public static NewEntity CreateForTodoItem(this ITimeTrackingContract contract, Int64 todoItemId, TimeEntryNew item)
        {
            return contract.CreateForTodoItem(todoItemId.ToString(), item);
        }

        public static TimeEntryNew New(this ITimeTrackingContract contract, Int64 projectId)
        {
            return contract.New(projectId.ToString());
        }

        public static TimeEntry Get(this ITimeTrackingContract contract, Int64 timeEntryId)
        {
            return contract.Get(timeEntryId.ToString());
        }

        public static TimeEntryNew Edit(this ITimeTrackingContract contract, Int64 timeEntryId)
        {
            return contract.Edit(timeEntryId.ToString());
        }

        public static void Update(this ITimeTrackingContract contract, Int64 timeEntryId, TimeEntryNew item)
        {
            contract.Update(timeEntryId.ToString(), item);
        }

        public static void Delete(this ITimeTrackingContract contract, Int64 timeEntryId)
        {
            contract.Delete(timeEntryId.ToString());
        }

        public static TimeEntries Report(this ITimeTrackingContract contract, Expression<Func<ReportCriteria, bool>> predicate)
        {
            var visitor = new ExpressionVisitor();
            var criteria = visitor.Visit(predicate.Body);
            return contract.Report(string.Format("report.xml?{0}", criteria.TrimStart("&".ToCharArray())));
        }
    }
}
